﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Benchmark.Core.Communication.Hub.Impl.Pipe;
using Benchmark.Core.Communication.Hub.Server.Impl.Pipe;
using Benchmark.Core.Communication.Model;


namespace Tcp.Performance.Toolkit.HubClient
{
    public class HubClient
    {
        readonly AsyncPipeClientEventArgument _client;
        

        public HubClient()
        {
            _client = new AsyncPipeClientEventArgument();
        }

        public void StartHubClient(string pipeName)
        {
            _client.Pipename = pipeName;
            _client.Connect(true);

        }
        

        public bool PutPipeInListenMode<TInitModelType>()
        {
            var req =  _client.ReadFromPipe<string>();
            var reqE = (HubModel.OperationType)Enum.Parse(typeof(HubModel.OperationType), req);
          

            switch (reqE)
            {

                case HubModel.OperationType.Test:
                    _client.ManageResponse<string>(req+"OK");
                    break;

                case HubModel.OperationType.Initialize:
                    var initModel= _client.ReadFromPipe<TInitModelType>();
                   //TODO: Initialize distributed operation on client machines operation can be async/sync
                    _client.ManageResponse<string>(req + "OK");
                    
                    break;


                case HubModel.OperationType.Execute:

                    //TODO: Execute distributed operation on client machines operation can be async/sync
                    Task.Factory.StartNew(async () =>
                    {
                        
                        
                   //  TODO: await execute the logic
                     
                    });

                    _client.ManageResponse<string>(req + "OK");
                    break;

                case HubModel.OperationType.Finish:
                  
                    _client.ManageResponse<string>(req + "OK");
                    return (true);
                    break;


                case HubModel.OperationType.Release:
                    _client.Relenquish();
                    break;

                default:
                    break;
            }

            return (false);

        }

        
    }
}
